package com.aabte.weather.weatherredis.weather;

import com.aabte.weather.weatherredis.city.CityDataService;
import com.aabte.weather.weatherredis.city.dto.City;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
public class WeatherDataSyncJob extends QuartzJobBean {

    @Resource
    private CityDataService cityDataService;

    @Resource
    private WeatherDataService weatherDataService;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        log.info("start sync weather data.");
        List<City> cities;
        try {
            cities = cityDataService.listCity();
        } catch (Exception e) {
            log.warn("get city list failed.", e);
            return;
        }

        for (City city : cities) {
            String cityId = city.getCityId();
            log.info("syncing weather data. cityId: {}", cityId);
            weatherDataService.syncDataByCityId(cityId);
        }
        log.info("end sync weather data.");
    }
}
